<template>
  <div class="register-background">
    <div class="register">
      <h2>用户注册</h2>
      <el-form ref="registerForm" :model="registerForm" label-width="100px">
        <el-form-item label="用户名">
          <el-input v-model="registerForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="registerForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="registerForm.email"></el-input>
        </el-form-item>
        <el-form-item label="身份">
          <el-select v-model="registerForm.role" placeholder="请选择身份">
            <el-option label="管理员" value="admin"></el-option>
            <el-option label="安防人员" value="security"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="验证码">
          <el-input v-model="registerForm.captcha">
            <el-button slot="append" @click="getCaptcha">获取验证码</el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="register">注册</el-button>
          <el-button @click="goToLogin">返回登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'UserRegister',
  data() {
    return {
      registerForm: {
        username: '',
        password: '',
        email: '',
        role: '',
        captcha: ''
      }
    };
  },
  methods: {
    register() {
      axios.post('http://localhost:8083/register', this.registerForm)
          .then(response => {
            console.log('注册成功:', response.data);
            this.$message.success('注册成功！请检查您的邮箱获取验证码。');
            this.goToLogin();
          })
          .catch(error => {
            console.error('注册失败:', error);
            this.$message.error('注册失败，请重试！');
          });
    },
    goToLogin() {
      this.$router.push('/login');
    },
    getCaptcha() {
      const email = this.registerForm.email;
      if (!email) {
        this.$message.error('请先填写邮箱');
        return;
      }

      axios.post('http://localhost:8083/send-verification-code', { email })
          .then(response => {
            console.log('验证码发送成功:', response.data);
            this.$message.success('验证码已发送到您的邮箱');
          })
          .catch(error => {
            console.error('验证码发送失败:', error);
            this.$message.error('验证码发送失败，请重试！');
          });
    }
  }
};
</script>

<style scoped>
.register-background {
  background-image: url('@/assets/background.jpg');
  background-size: cover;
  background-position: center;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.register {
  width: 400px;
  padding: 20px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.register h2 {
  text-align: center;
  margin-bottom: 20px;
}
</style>
